package com.xxl.job.admin.service;

import com.xxl.job.core.biz.model.ReturnT;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Service
public class XxlJobExecutorService {

    @Value("${xxl.job.accessToken}")
    private String accessToken;
    // 调度中心的 URL
    private static final String XXL_JOB_TRIGGER_URL = "http://192.168.1.2:8089/xxl-job-admin/jobinfo/trigger";


    // 调度指定服务器上的XXL-JOB任务
    public String triggerDownCronJobTask(int jobId, String schedule, String command, String serverIp, String sessionId,String serverUser) {
        try {
            RestTemplate restTemplate = new RestTemplate();

            // 构建请求体
            MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
            requestBody.add("id", String.valueOf(jobId));
            requestBody.add("executorParam", schedule + "," + command + "," + serverIp + "," + sessionId + "," + serverUser);  // 拼接参数
            requestBody.add("addressList", "http://"+serverIp+":9999");  // 目标服务器IP
            requestBody.add("accessToken", "default_token");  // Access Token

            // 输出请求体内容
            System.out.println("调度执行器请求体: " + requestBody);

            // 设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            headers.add(HttpHeaders.COOKIE, "XXL_JOB_LOGIN_IDENTITY=" + sessionId);  // 传入cookie信息

            HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(requestBody, headers);
            System.out.println(entity);

            // 发送 POST 请求到调度中心
            ResponseEntity<ReturnT> response = restTemplate.postForEntity(XXL_JOB_TRIGGER_URL, entity, ReturnT.class);

            // 检查响应结果
            if (response != null && response.getBody() != null) {
                ReturnT result = response.getBody();
                System.out.println("调度执行器返回结果: " + result);
                return result.getCode() == ReturnT.SUCCESS_CODE ? "success" : "failed";
            } else {
                System.out.println("调度执行器返回的响应为空");
                return "failed";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "failed";
        }
    }

    // 调度指定服务器上的XXL-JOB任务
    public String triggerUpdateCronJobTask(int jobId,
                                           String schedule,String newSchedule,
                                           String command, String newCommand,String serverIp,
                                           String sessionId,String serverUser) {
        try {
            RestTemplate restTemplate = new RestTemplate();

            // 构建请求体
            MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
            requestBody.add("id", String.valueOf(jobId));
            requestBody.add("executorParam", schedule + "," + newSchedule + "," + command + "," +newCommand +"," + serverIp + "," + sessionId + "," + serverUser);  // 拼接参数
            requestBody.add("addressList", "http://"+serverIp+":9999");  // 目标服务器IP
            requestBody.add("accessToken", "default_token");  // Access Token

            // 输出请求体内容
            System.out.println("调度执行器请求体: " + requestBody);

            // 设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            headers.add(HttpHeaders.COOKIE, "XXL_JOB_LOGIN_IDENTITY=" + sessionId);  // 传入cookie信息

            HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(requestBody, headers);
            System.out.println(entity);

            // 发送 POST 请求到调度中心
            ResponseEntity<ReturnT> response = restTemplate.postForEntity(XXL_JOB_TRIGGER_URL, entity, ReturnT.class);

            // 检查响应结果
            if (response != null && response.getBody() != null) {
                ReturnT result = response.getBody();
                System.out.println("调度执行器返回结果: " + result);
                return result.getCode() == ReturnT.SUCCESS_CODE ? "success" : "failed";
            } else {
                System.out.println("调度执行器返回的响应为空");
                return "failed";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "failed";
        }
    }


    // 初始化定时任务
    public String triggerInitCronJobTask(int jobId,String sessionId) {
        try {
            RestTemplate restTemplate = new RestTemplate();

            // 构建请求体
            MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
            requestBody.add("id", String.valueOf(jobId));
            requestBody.add("executorParam",sessionId);  // 拼接参数
            requestBody.add("addressList", "");  // 目标服务器IP
            requestBody.add("accessToken", "default_token");  // Access Token

            // 输出请求体内容
            System.out.println("调度执行器请求体: " + requestBody);

            // 设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            headers.add(HttpHeaders.COOKIE, "XXL_JOB_LOGIN_IDENTITY=" + sessionId);  // 传入cookie信息

            HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(requestBody, headers);
            System.out.println(entity);

            // 发送 POST 请求到调度中心
            ResponseEntity<ReturnT> response = restTemplate.postForEntity(XXL_JOB_TRIGGER_URL, entity, ReturnT.class);

            // 检查响应结果
            if (response != null && response.getBody() != null) {
                ReturnT result = response.getBody();
                System.out.println("调度执行器返回结果: " + result);
                return result.getCode() == ReturnT.SUCCESS_CODE ? "success" : "failed";
            } else {
                System.out.println("调度执行器返回的响应为空");
                return "failed";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "failed";
        }
    }


}
